var options={
	height:'full-124',
	page:true,
	id:'id',
	limit:10,
	path:'',
	url:'',
	postData:{},
	addurl:'add',
	editurl:'edit',
	delurl:'delete',
	toolbar: 'default'
};
var LayUiTable=function(data){
	var that=this,$=layui.$,tableIns;
	this.currRow=null;
	this.ops=$.extend({},options,data);
	//$.extend(options,data);
	if(!this.ops.url){
		this.ops.url=this.ops.path+'/page';
	}
	this.init=function(){
		layui.use(['table','form'],function(){
			var form=layui.form;
			form.verify({
				integer:function(value,item){
					if(!/^\+?[1-9][0-9]*$/.test(value)){
						return "必须输入一个正整数";
					}
				}
			});
			table=layui.table;
			tableIns=table.render(that.ops);
			//表格的选中行事件处理
			table.on('row',function(obj){
				if(that.currRow&&that.currRow.data.id===obj.data.id){
					that.currRow=null;
					obj.tr.removeClass('selected');
				}else{
					that.currRow=obj;
					obj.tr.addClass('selected').siblings().removeClass('selected');
				}
			});
			//表格的工具栏事件处理
			table.on('toolbar',function(obj){
				switch(obj.event){
				case 'add':
					that.add();
					break;
				case 'delete':
					that.del();
					break;
				case 'update':
					that.edit();
					break;
				}
			});
			
		});
	}

	//新增操作
	this.add=function(){
		var addurl=that.ops.path+(that.ops.path.length>0?'/':'')+that.ops.addurl;
		var saveurl=that.ops.path+(that.ops.path.length>0?'/':'')+'save';
		$.get(addurl,that.ops.postData,function(data){
		      layer.open({
		    	  id:'add',
		    	  type:1,
		    	  title:'新增',
		    	  content:data,
		    	  btn:['提交','退出'],
		    	  //虽然什么也不做也一定要定义的，否则，校验出错也会关闭对话框
		    	  yes:function(index,layero){

		    	  },
		    	  success:function(layero,index){
		    		  layui.use('form',function(){
		    			 var form=layui.form;
		    			 layero.addClass('layui-form');
		    			 var submitBtn=layero.find('.layui-layer-btn0');
		    			 submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
		    			 layero.keydown(function(e){
		    				 if(e.keyCode==13){
		    					 submitBtn.click();
		    				 }
		    			 });
		    			 
		    			 form.on('submit(formVerify)',function(data){
		    				$.post(saveurl,data.field,function(result){
		    					if(result.success){
		    						that.currRow=null;
		    						layer.close(index);
		    						table.reload('id',{toolbar:'default',where:that.ops.postData});
		    					}
		    					layer.msg(result.msg,{offset:'rb'});
		    				});
		    				return false;
		    			 });
		    		  });
		    	  }
		      });
	 		  
	 	  },'html');
	};
	//修改操作
	this.edit=function(){
		var editurl=that.ops.path+(that.ops.path.length>0?'/':'')+that.ops.editurl;
		var saveurl=that.ops.path+(that.ops.path.length>0?'/':'')+'save';
		var postData=$.extend({},that.ops.postData,{id:that.currRow?that.currRow.data.id:''});
      	if(that.currRow){
      		$.get(editurl,postData,function(html){
			      layer.open({
			    	  id:'user1',
			    	  type:1,
			    	  shadeClose:true,
			    	  title:'修改',
			    	  content:html,
			    	  btn:['提交','退出'],
			    	  yes:function(index,layero){
			    	  },
			    	  //弹出层打开成功的回调
			    	  success:function(layero,index){
			    		  layui.use('form',function(){
			    			  var form=layui.form;
			    			  //让‘提交’按钮有提交功能
				    		  layero.addClass('layui-form');
				    		  var submitBtn=layero.find('.layui-layer-btn0');
				    		  submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
				    		  //在弹出层里绑定回车就提交表单
				    		  layero.keydown(function(e){
				    			 if(e.keyCode==13){
				    				 submitBtn.click();
				    			 }
				    		  });
				    		  
				    		  //绑定表单提交事件，先校验，通过就ajax提交
				    		  form.on('submit(formVerify)',function(data){
				    			  $.post(saveurl,data.field,function(result){
				    				  if(result.success){
				    					  that.currRow=null;
				    					  table.reload('id',{toolbar: 'default',where:that.ops.postData});
				    					  //tableIns.reload();
				    					  layer.close(index);
				    				  }
				    				  layer.msg(result.msg,{offset:'rb'});
				    			  });
				    			  return false;
				    		  });
			    		  });
			    	  },
			      });
      			
      		},'html');
      	}else{
      		layer.alert('请先选择一行数据再进行修改操作！');
      	}
	};
	//删除事件
	this.del=function(){
		var delurl=that.ops.path+(that.ops.path.length>0?'/':'')+that.ops.delurl;
	      if(that.currRow){
	    	  layer.confirm('真的删除行么?', function(index){
	    	    //向服务端发送删除指令
	    	  	$.post(delurl,{id:that.currRow.data.id},function(data){
		    	  	if(data.success){
		    	  		that.currRow.del(); //删除对应行（tr）的DOM结构，并更新缓存
			    	   	layer.close(index);
		    	  	}
					layer.msg(data.msg,{offset:'rb'});			    	  		
	    	  	}); 
	      	});
	      }else{
	    	  layer.alert('请先选择一条数据再进行删除操作！');
	      }
	};
}